package easy;

import java.util.Objects;

public class Solution_226 {

    public static TreeNode invertTree(TreeNode root) {
        if (root==null) {
            return null;
        }
        TreeNode left = root.left, right = root.right;
        if (Objects.nonNull(left)) {
            invertTree(left);
        }
        if (Objects.nonNull(right)) {
            invertTree(right);
        }
        root.left = right;
        root.right = left;
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(1);
//        root.left.right = new TreeNode(3);
        root.right = new TreeNode(7);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(9);
        TreeNode treeNode = invertTree(root);
        System.out.println();
    }

    public static class TreeNode {

        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
